<?php
defined('IN_TS') or die('Access Denied.');
//用户注册
switch($ts){
	case "":
		
		$title = '注册';
		
		//调出省份数据
		$arrProvince = AppCacheRead('location','province.php');
		$arrCitys = AppCacheRead('location','city.php');
		
		$arrCity = $arrCitys['110000'];
		
		//调出第一个区县
		$arrAreas = AppCacheRead('location','area.php');
		$arrArea = $arrAreas['110100'];
		
		include TmooTemplate("register");
		break;

	case "do":
		if (IS_POST) {
			$email		= trim($_POST['email']);
			$pwd			= trim($_POST['pwd']);
			$repwd		= trim($_POST['repwd']);
			$username		= t($_POST['username']);
			$provinceid = $_POST['provinceid'];
			$cityid = $_POST['cityid'];
			$areaid = $_POST['areaid'];
			
			//是否开启邀请注册
			if($TS_APP['options']['isregister']=='1'){
			
				$invitecode = trim($_POST['invitecode']);
				if($invitecode == '') qiMsg("邀请码不能为空！");
				$codeNum = $DB->once_num_rows("select * from ".dbprefix."app_user_invites where invitecode='$invitecode' and isused='0'");
				if($codeNum == '0') qiMsg("邀请码无效，请更换邀请码！");
			
			}

			$isEmail = $DB->once_num_rows("SELECT * FROM ".dbprefix."app_user WHERE email='$email'");
			$isusername = $DB->once_num_rows("select * from ".dbprefix."app_user_info where username='$username'");
			
			
			if(empty($email) || empty($pwd) || empty($repwd) || empty($username)){
				qiMsg('所有必选项都不能为空！');
			}elseif(valid_email($email) == false){
				qiMsg('Email邮箱输入有误!');
			}elseif($isEmail != '0'){
				qiMsg('Email已经注册^_^');
			}elseif($pwd != $repwd){
				qiMsg('两次输入密码不正确！');
			}elseif(strlen($username) < 4 || strlen($username) > 20){
				qiMsg('姓名长度必须在4和20之间!');
			}elseif($username > 0){
				qiMsg("用户名已经存在，请换个用户名！");
			}else{
				
				$DB->query("INSERT INTO ".dbprefix."app_user (`pwd` , `email`) VALUES ('".md5($pwd)."', '$email');");
				
				$userid = $DB->insert_id();
				
				//积分
				$DB->query("insert into ".dbprefix."app_user_scores (`userid`,`scorename`,`score`,`addtime`) values ('".$userid."','注册','1000','".time()."')");
				
				//用户信息
				$arrData = array(
					'userid'			=> $userid,
					'username' 	=> $username,
					'email'		=> $email,
					'provinceid'	=> $provinceid,
					'cityid'			=> $cityid,
					'areaid'		=> $areaid,
					'ip'			=> getIp(),
					'count_score'	=> '1000',
					'addtime'	=> time(),
					'uptime'	=> time(),
				);
				
				//插入用户信息
				$DB->insertArr($arrData,'app_user_info');
				
				//默认加入小组
				$isgroup = $DB->once_fetch_assoc("select optionvalue from ".dbprefix."app_user_options where optionname='isgroup'");
				
				if($isgroup['optionvalue'] != ''){
				
					$arrGroup = explode(',',$TS_APP['options']['isgroup']);
					
					foreach($arrGroup as $item){
					
						$groupusernum = $DB->once_num_rows("select * from ".dbprefix."app_group_users where `userid`='".$userid."' and `groupid`='".$groupid."'");
						
						if($groupusernum == '0'){
							$DB->query("insert into ".dbprefix."app_group_users (`userid`,`groupid`,`addtime`) values('".$userid."','".$item."','".time()."')");
							//统计更新
							$count_user = $DB->once_num_rows("select * from ".dbprefix."app_group_users where groupid='".$item."'");
							$DB->query("update ".dbprefix."app_group set `count_user`='".$count_user."' where groupid='".$item."'");
						}
					}
				}
				
				$userData = $DB->once_fetch_assoc("select * from ".dbprefix."app_user_info where userid='$userid'");
				
				$_SESSION['tsuser']	= $userData;
				
				//发送系统消息(恭喜注册成功)
				$msg_userid = '0';
				$msg_touserid = $userid;
				$msg_content = '亲爱的 '.$username.' ：<br />您成功加入了 '
											.$TS_SITE['base']['site_title'].'<br />在遵守本站的规定的同时，享受您的愉快之旅吧!';
				aac('message',$DB)->sendmsg($msg_userid,$msg_touserid,$msg_content);
				
				//注销邀请码
				if($TS_APP['options']['isregister']=='1'){
					$DB->query("update ".dbprefix."app_user_invites set `isused`='1' where invitecode='$invitecode'");
				}
				
				//跳转
				header('Location: index.php');
				
			}
		}
		break;
}